package com.qingxin.wuye.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyPropertyCompany;

@Service
public class WyPropertyCompanyService extends BaseService<WyPropertyCompany, String> {

	
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getCompanyData(LoginUser loginUser, boolean isParent){
		List<JsTreeNode> rslist = new ArrayList<>();
		String hql = "select new com.qingxin.wuye.entity.WyPropertyCompany(companyid, companyName)" +
				" from WyPropertyCompany order by companyName";
		List<WyPropertyCompany> list = listByHql(hql);
		if(list != null){
			for(WyPropertyCompany company : list){
				JsTreeNode treeNode = new JsTreeNode(company.getCompanyid(), company.getCompanyName(), SystemCfg.UI_DATATYPE_COMPANY, "companyid", isParent);
				rslist.add(treeNode);
			}
		}
		return rslist;
	}
	
	
	public Pager list(PageParam pageParam, LoginUser loginUser, HttpServletRequest request){
		Querier querier = Querier.create("from WyPropertyCompany order by createTime desc");
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加企业信息
	 * @param material
	 * @param typeid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyPropertyCompany company, LoginUser loginUser, HttpServletRequest request){
		if(StringUtils.isNotEmpty(company.getBusinessImg())){
			UploadFile.moveTmpTo(company.getBusinessImg(), UploadFile.DIR_WUYE);
		}
		
		company.setCreateTime(new Date());
		company.setUpdateTime(new Date());
		company.setCreateBy(loginUser.getLogin_name());
		company.setUpdateBy(loginUser.getLogin_name());
		
		this.save(company);
		
		return null;
	}
	
	
	/**
	 * 修改企业信息
	 * @param company
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyPropertyCompany company, LoginUser loginUser, HttpServletRequest request){
		WyPropertyCompany old = getSafeEntity(company.getCompanyid());
		
		if(StringUtils.isNotEmpty(company.getBusinessImg()) && !company.getBusinessImg().equals(old.getBusinessImg())){
			UploadFile.moveTmpTo(company.getBusinessImg(), UploadFile.DIR_WUYE);
		}
		
		String fields = "companyName,corporatePerson,contactPerson,phone,address,businessNo,businessImg,remark";
		BeanUtil.setTargetFields(old, company, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyPropertyCompany> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		return null;
	}
	
	
	/**
	 * 删除
	 * @param companyids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] companyids, LoginUser loginUser, HttpServletRequest request){
		for(String companyid : companyids){
			WyPropertyCompany company = get(companyid);
			if(company==null){
				continue;
			}
			this.delete(company);
		}
		
		return null;
	}
	
	
	/**
	 * 判断指定名称是否被使用
	 * @param companyName
	 * @param companyid
	 * @return
	 */
	public boolean isNameDuplicate(String companyName, String companyid){
		if(StringUtils.isEmpty(companyid)){
			Object obj = getByHql("select companyid from WyPropertyCompany where companyName=? ", companyName);
			return obj!=null;
		}else {
			Object obj = getByHql("select companyid from WyPropertyCompany where companyid<>? and companyName=? ", companyid, companyName);
			return obj!=null;
		}
	}
	
	
	
	
	
}
